package com.xhwl.logistics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.dto.cent.eesd.EnvStatisticsReqV2DTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.bi.DwdDimChargeMetrics;
import com.xhwl.logistics.bo.VehicleAccessRecordBO;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordBO;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordSecondBO;
import com.xhwl.logistics.component.VehicleConfigProperty;
import com.xhwl.logistics.converter.ToPageBO;
import com.xhwl.logistics.converter.ToPageVO;
import com.xhwl.logistics.converter.ToVehicleAccessRecordVO;
import com.xhwl.logistics.converter.mapstruct.VehicleAccessRecordConverter;
import com.xhwl.logistics.dao.VehcCarRecordDao;
import com.xhwl.logistics.dto.query.VehicleAccessRecordDTO;
import com.xhwl.logistics.dto.query.VehicleAccessRecordSecondDTO;
import com.xhwl.logistics.entity.VehicleAccessRecord;
import com.xhwl.logistics.entity.excel.VehicleAccessReportFormsBiExcel;
import com.xhwl.logistics.service.IVehicleAccessRecordService;
import com.xhwl.logistics.service.cent.IBIVehicleService;
import com.xhwl.logistics.service.cent.QueryProjectService;
import com.xhwl.logistics.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;

import java.util.ArrayList;
import java.util.List;

//@RestController
public class VehicleAccessRecordController {

    @Autowired
    private VehicleConfigProperty vehicleConfigProperty;

    @Autowired
    private IVehicleAccessRecordService vehicleAccessRecordService;

    @Autowired
    private VehicleAccessRecordConverter vehicleAccessRecordConverter;

    @Autowired
    private ToVehicleAccessRecordVO toVehicleAccessRecordVO;

    @Autowired
    private ToPageBO toPageBO;

    @Autowired
    private ToPageVO toPageVO;

    @Autowired
    VehcCarRecordDao vehcCarRecordDao;

    @Autowired
    QueryProjectService queryProjectService;

    @Autowired
    IBIVehicleService ibiVehicleService;

    @PostMapping("/carRecord/page")
    public ResultJson<PageVO<VehicleAccessRecordAnotherVO>> accessRecord(@RequestBody VehicleAccessRecordDTO vehicleAccessRecordDTO) {

        QueryVehicleAccessRecordBO queryVehicleAccessRecordBO = vehicleAccessRecordConverter.toQueryVehicleAccessRecordBO(vehicleAccessRecordDTO, vehicleConfigProperty.getDay(), vehicleConfigProperty.getHour(), vehicleConfigProperty.getMinute());
        QueryVehicleAccessRecordSecondBO queryVehicleAccessRecordSecondBO = vehicleAccessRecordConverter.vehicleAccessRecordDTO2QueryVehicleAccessRecordSecondBO(vehicleAccessRecordDTO);

        IPage<VehicleAccessRecordBO> vehicleAccessRecordBOIPage;
        if (queryVehicleAccessRecordSecondBO.isPlateNumberDuplicateRemoval()) {
            PageBO pageBO = toPageBO.toPageBOForAccessRecordPlateNumberDuplicateRemoval(vehicleAccessRecordDTO);
            vehicleAccessRecordBOIPage = vehicleAccessRecordService.accessRecordForPlateNumberDuplicateRemoval(queryVehicleAccessRecordSecondBO, pageBO);
        } else {
            PageBO pageBO = toPageBO.toPageBOForAccessRecord(vehicleAccessRecordDTO);
            vehicleAccessRecordBOIPage = vehicleAccessRecordService.accessRecord(queryVehicleAccessRecordBO, pageBO);
        }

        return ResultJson.successWithResult(toPageVO.toPageVO(vehicleAccessRecordBOIPage, toVehicleAccessRecordVO.toVehicleAccessRecordAnotherVOList(vehicleAccessRecordBOIPage.getRecords())));
    }

    @PostMapping("/carRecord/statistics")
    public ResultJson<PageVO<VehicleAccessReportFormsBiExcel>> accessRecordStatistics(@RequestBody AccessRecordQuery query) {

        ProjectBO projectBO = queryProjectService.queryProject(query.getProjectId());

        IPage<AccessRecordStatisticsVo> recordStatisticsVoIPage = vehcCarRecordDao.queryRecordStatistics(query.toPage(), query);

        List<VehicleAccessReportFormsBiExcel> vehicleAccessReportFormsBiExcels = new ArrayList<>();

        EnvStatisticsReqV2DTO dto = new EnvStatisticsReqV2DTO();
        dto.setProjectId(query.getProjectId());
        dto.setDate(query.getDate());
//        dto.setProjectName(projectBO.getProjectName());
//        dto.setPageNumber(1);
//        dto.setPageSize(999);

        if (query.getMode() == null || query.getMode() == 0) {
            dto.setTimeRange("TOTAL");
        } else if (query.getMode() == 1) {
            dto.setTimeRange("month");
        } else if (query.getMode() == 2) {
            dto.setTimeRange("year");
        } else {
            throw new IllegalArgumentException("mode参数非法");
        }

        ResultJson<Page<DwdDimChargeMetrics>> resultJson = ibiVehicleService.chargeTrendV2(dto);

//        ResultJson<Page<DwdDimChargeMetrics>> resultJson = new ResultJson<>();
//        Page<DwdDimChargeMetrics> page = new Page<DwdDimChargeMetrics>();
//        List list = new ArrayList<DwdDimChargeMetrics>();
//
//        for (int i = 0; i < 10; i++) {
//            DwdDimChargeMetrics param = new DwdDimChargeMetrics();
//            param.setDt("2021-04-2" + i);
//            param.setConnector(i);
//            list.add(param);
//        }
//
//        page.setRecords(list);
//        resultJson.setResult(page);

        if (recordStatisticsVoIPage != null && recordStatisticsVoIPage.getRecords() != null) {
            recordStatisticsVoIPage.getRecords().forEach(it -> {

                VehicleAccessReportFormsBiExcel vehicleAccessReportFormsBiExcel = new VehicleAccessReportFormsBiExcel();
                vehicleAccessReportFormsBiExcel.setDate(it.getEnterDate() == null ?  it.getExistDate() : it.getEnterDate());
                vehicleAccessReportFormsBiExcel.setProjectName(projectBO.getProjectName());
                vehicleAccessReportFormsBiExcel.setCarInCount(it.getInTypeCount() == null ? 0 : it.getInTypeCount());
                vehicleAccessReportFormsBiExcel.setCarOutCount(it.getOutTypeCount() == null ? 0 : it.getOutTypeCount());
                vehicleAccessReportFormsBiExcel.setCarInOutCount(vehicleAccessReportFormsBiExcel.getCarInCount() + vehicleAccessReportFormsBiExcel.getCarOutCount());
                vehicleAccessReportFormsBiExcel.setLimitTimeInOutCount((it.getInLimitTimeCardCount() == null ? 0 : it.getInLimitTimeCardCount()) + (it.getOutLimitTimeCardCount() == null ? 0 : it.getOutLimitTimeCardCount()));
                vehicleAccessReportFormsBiExcel.setFullTimeInOutCount((it.getInFullTimeCardCount() == null ? 0 : it.getInFullTimeCardCount()) + (it.getOutFullTimeCardCount() == null ? 0 : it.getOutFullTimeCardCount()));
                vehicleAccessReportFormsBiExcel.setTempCarInOutCount((it.getInTempCardCount() == null ? 0 : it.getInTempCardCount()) + (it.getOutTempCardCount() == null ? 0 : it.getOutTempCardCount()));
                vehicleAccessReportFormsBiExcel.setTramCarInOutCount(0);
                if (resultJson != null && resultJson.getResult() != null && resultJson.getResult().getRecords() != null) {
                    resultJson.getResult().getRecords().stream().filter(dwdDimChargeMetrics -> {
                        return dwdDimChargeMetrics.getDt().equals(vehicleAccessReportFormsBiExcel.getDate());
                    }).findFirst().ifPresent(value -> {
                        vehicleAccessReportFormsBiExcel.setTramCarInOutCount(value.getConnectorA());
                    });
                }
                vehicleAccessReportFormsBiExcels.add(vehicleAccessReportFormsBiExcel);
            });
        }


        return ResultJson.successWithResult(toPageVO.toPageVO(recordStatisticsVoIPage, vehicleAccessReportFormsBiExcels));
    }

    @PostMapping("list")
    public List<VehicleAccessRecord> list(@RequestBody VehicleAccessRecord vehicleAccessRecord) {
        List<VehicleAccessRecord> vehcCarRecord = vehicleAccessRecordService.list(vehicleAccessRecord);
        return vehcCarRecord;
    }

    @PostMapping("/carInOutRecord/page")
    public ResultJson<PageVO<VehicleAccessRecordSecondVO>> accessRecordForInOut(@RequestBody VehicleAccessRecordSecondDTO vehicleAccessRecordSecondDTO) {
        IPage<VehicleAccessRecordSecondVO> vehicleAccessRecordSecondVOIPage = vehicleAccessRecordService.accessRecordForInOut(vehicleAccessRecordSecondDTO);
        return ResultJson.successWithResult(toPageVO.toPageVO(vehicleAccessRecordSecondVOIPage, vehicleAccessRecordSecondVOIPage.getRecords()));
    }

    /**
     * 近30日进出趋势
     * @param query
     * @return
     */
    @PostMapping("/car-record/trend-thirty-days")
    public ResultJson<List<AccessRecordTrendVo>> accessRecordTrendThirtyDays(@RequestPart("query") AccessTrendQuery query) {
        return ResultJson.success(vehicleAccessRecordService.accessRecordTrendThirtyDays(query));
    }
}
